#!/usr/local/bin/python
# -*- encoding:utf-8 -*-

import sys
import random
human_labeled = open(sys.argv[1], 'r');

human_labeled_dict = {};
for line in human_labeled:
    li  = line.split('\t', 4);
    queryString = li[1];
    appID = li[2];
    labeled = int(li[3]);
    if human_labeled_dict.has_key(queryString):
        human_labeled_dict[queryString].append((appID, labeled));
    else:
        human_labeled_dict[queryString] = [];
        human_labeled_dict[queryString].append((appID, labeled));
        

human_labeled.close();

def checkValid(value):
    s = sum([a for c, a in value]);
    if s == 0:
        return False;
    else:
        return True;

format_labeled = open(sys.argv[2], 'w');

for key, value in human_labeled_dict.items():
    if checkValid(value):
        value = [(v, k) for k, v in value];
        value.sort();
        s = '%s|%s\n' % (key, '|'.join(['%d:%s:%d' %(pos, item[1], item[0] ) for pos, item in enumerate(value)]));
        format_labeled.write(s);

format_labeled.close();
